package com.weijin.web.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.weijin.mapper.ChatHistoryMapper;
import com.weijin.model.User;
import com.weijin.service.IUserService;
import com.weijin.vo.PageVo;
import com.weijin.vo.RestResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 用户接口
 *
 * @Author WeiJin
 * @Version 1.0
 * @Date 2025/3/18 14:30
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@Slf4j
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public RestResultVo<String> add(@RequestBody User user) {
        String id = userService.addUser(user);
        if (StringUtils.isNotBlank(id)) {
            return RestResultVo.ok(id);
        }
        return RestResultVo.failed("添加失败");
    }

    @ApiOperation("修改用户")
    @PostMapping("/edit")
    public RestResultVo<User> edit(@RequestBody User user) {
        User editUser = userService.editByUserId(user);
        if (Objects.nonNull(editUser)) {
            return RestResultVo.ok(editUser);
        }
        return RestResultVo.failed("修改失败");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "id", value = "用户id")
    public RestResultVo<Integer> delete(@RequestParam("id") String id) {
        int deleted = userService.deleteOne(id);

        if (deleted > 0) {
            return RestResultVo.ok(deleted);
        }
        return RestResultVo.failed("删除失败");
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/detail")
    public RestResultVo<User> detail(@RequestParam("id") String id) {
        User user = userService.detail(id);

        if (Objects.nonNull(user)) {
            return RestResultVo.ok(user);
        }
        return RestResultVo.failed("获取详情失败");
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = false),
            @ApiImplicitParam(name = "pageNumber", value = "页码", required = false, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, defaultValue = "10")
    })
    public RestResultVo<PageVo<User>> page(@RequestParam(value = "username", required = false) String username,
                                           @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageVo<User> page = userService.page(username, pageNumber, pageSize);
        if (Objects.nonNull(page)) {
            return RestResultVo.ok(page);
        }
        return RestResultVo.failed("分页查询失败");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public RestResultVo<User> login(@RequestParam("username") String username) {
        User user = userService.login(username);
        if ((Objects.nonNull(user))) {
            return RestResultVo.ok(user);
        }
        return RestResultVo.failed("用户不存在");
    }
}
